import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query, Req, Res } from '@nestjs/common';
import { ParseIntArrayPipe } from '@admin/common/pipe/parse-int-array.pipe';
import Result from '@admin/common/result/Result';
import { ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { RequirePermission } from '@admin/common/decorator/require-premission.decorator';
import { nowDateTime } from '@admin/common/utils';
import { JpBannerService } from './service/jp-banner.service';
import { QueryJpBannerDto, CreateJpBannerDto, UpdateJpBannerDto } from './dto/index';
import { Response } from 'express';
import { JpBanner } from '@prismaClient';
import { TableDataInfo } from '@admin/common/domain/TableDataInfo';
import { QueryParams } from '@admin/common/domain/queryDomain';
@ApiTags('家谱Banner')
@ApiBearerAuth()
@Controller('web/banner')
export class JpBannerController {
  constructor(private bannerService: JpBannerService) {}
  @ApiOperation({ summary: '查询家谱Banner列表' })
  @ApiQuery({ type: QueryParams })
  @ApiResponse({ type: TableDataInfo<JpBanner> })
  @RequirePermission('web:banner:list')
  @Get('/list')
  async listBanner(@Query() q: QueryJpBannerDto): Promise<TableDataInfo<JpBanner>> {
    return Result.TableData(await this.bannerService.selectBannerList(q));
  }
  @ApiOperation({ summary: '查询家谱Banner所有' })
  @ApiResponse({ type: Result<JpBanner[]> })
  @RequirePermission('web:banner:list')
  @Get('/data')
  async listBannerData(): Promise<Result<JpBanner[]>> {
    return Result.ok(await this.bannerService.selectBannerAll());
  }
  @ApiOperation({ summary: '导出家谱Bannerxlsx文件' })
  @RequirePermission('web:banner:export')
  @Get('/export')
  async export(@Res() res: Response): Promise<void> {
    return this.bannerService.exportBanner(res);
  }
  @ApiOperation({ summary: '查询家谱Banner详细' })
  @ApiResponse({ type: Result<JpBanner> })
  @RequirePermission('web:banner:list')
  @Get('/:id')
  async getBanner(@Param('id', ParseIntPipe) id: number): Promise<Result<JpBanner>> {
    return Result.ok(await this.bannerService.selectBannerById(id));
  }
  @ApiOperation({ summary: '新增家谱Banner' })
  @ApiResponse({ type: Result<JpBanner> })
  @ApiBody({ type: CreateJpBannerDto })
  @RequirePermission('web:banner:add')
  @Post('/')
  async addBanner(@Body() jpBanner: CreateJpBannerDto, @Req() req): Promise<Result<JpBanner>> {
    jpBanner['createTime'] = nowDateTime();
    jpBanner['updateTime'] = nowDateTime();
    return Result.ok(await this.bannerService.addBanner(jpBanner));
  }
  @ApiOperation({ summary: '修改家谱Banner' })
  @ApiResponse({ type: Result<any> })
  @ApiBody({ type: UpdateJpBannerDto })
  @RequirePermission('web:banner:edit')
  @Put('/')
  async updateBanner(@Body() jpBanner: UpdateJpBannerDto, @Req() req): Promise<Result<any>> {
    jpBanner['updateTime'] = nowDateTime();
    await this.bannerService.updateBanner(jpBanner);
    return Result.ok('修改成功！');
  }
  @ApiOperation({ summary: '删除家谱Banner' })
  @ApiResponse({ type: Result<any> })
  @RequirePermission('web:banner:remove')
  @Delete('/:ids')
  async delBanner(@Param('ids', ParseIntArrayPipe) ids: number[]): Promise<Result<any>> {
    const { count } = await this.bannerService.deleteBannerByIds(ids);
    return Result.toAjax(count);
  }
}
